/**
 * Copyright 2005-2012 brin.com
 * All rights reserved.
 * 
 * @project
 * @author Brin
 * @version 1.0
 * @date 2012-12-22
 */
package com.brin.code.common;

import java.util.ArrayList;
import java.util.List;

import org.springframework.mail.SimpleMailMessage;

import com.brin.core.util.StringUtil;

/**
 * 发送邮件实体类
 * 
 * @author Brin
 *
 */
public class EmailServiceImpl implements EmailService {

	@Override
public void sendEmail(String subject, String text, List<String> toEmail) {
		
		if (StringUtil.isBlank(subject) || toEmail == null || toEmail.isEmpty()) return;
		
		List<SimpleMailMessage> smms = getMessages(subject, text, toEmail);
		for (int i = 0; i < smms.size(); i++) {
			new Thread(new SendMailRunnable(smms.get(i))).start();
		}
	}
	
	/**
	 * 拼装邮件内容列表
	 * 
	 * @param subject
	 * @param text
	 * @param toEmail
	 * @return
	 */
	private List<SimpleMailMessage> getMessages(String subject, String text, List<String> toEmail) {
		List<SimpleMailMessage> lists = new ArrayList<SimpleMailMessage>();
		for (String str : toEmail) {
			SimpleMailMessage smg = new SimpleMailMessage();
			smg.setSubject(subject);
			smg.setTo(str);
			smg.setText(text);
			lists.add(smg);
		}
		
		return lists;
	}

}
